package com.skyme.automation.generate;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.skyme.automation.generate.common.ImageKey;
import com.skyme.automation.generate.common.ResourceString;
import com.skyme.automation.generate.preferences.EasyExplorePreferencePage;

/**
 * The activator class controls the plug-in life cycle
 */
public class GeneratePlugin extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.skyme.automation.generate"; //$NON-NLS-1$

	// The shared instance
	private static GeneratePlugin plugin;

	private ResourceBundle resourceBundle;

	/**
	 * The constructor
	 */
	public GeneratePlugin() {
		plugin = this;
		try {
			resourceBundle = ResourceBundle
					.getBundle("org.sf.easyexplore.EasyExplorePluginResources");
		} catch (MissingResourceException x) {
			// throw new NullPointerException("unnable to load resources
			// org.sf.easyexplore.EasyExplorePluginResources");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static GeneratePlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	/**
	 * Returns the workspace instance.
	 */
	public static IWorkspace getWorkspace() {
		return ResourcesPlugin.getWorkspace();
	}

	/**
	 * Returns the string from the plugin's resource bundle, or 'key' if not
	 * found.
	 */
	public static String getResourceString(String key) {
		ResourceBundle bundle = GeneratePlugin.getDefault().getResourceBundle();
		String res = null;
		try {
			res = bundle.getString(key);
		} catch (MissingResourceException e) {
			res = key;
		}
		return res;
	}

	/**
	 * Returns the plugin's resource bundle,
	 */
	public ResourceBundle getResourceBundle() {
		return resourceBundle;
	}
	
	@Override
	protected void initializeImageRegistry(ImageRegistry reg) {
		super.initializeImageRegistry(reg);
		
		reg.put(ImageKey.ERROR, loadImageDescriptor("icon/error.gif"));
	}
	
	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	private static ImageDescriptor loadImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	static public void log(Object msg) {
		ILog log = GeneratePlugin.getDefault().getLog();
		Status status = new Status(IStatus.INFO, GeneratePlugin.PLUGIN_ID,
				IStatus.ERROR, msg + "\n", null);
		log.log(status);
	}

	static public void log(Throwable ex) {
		ILog log = GeneratePlugin.getDefault().getLog();
		StringWriter stringWriter = new StringWriter();
		ex.printStackTrace(new PrintWriter(stringWriter));
		String msg = stringWriter.getBuffer().toString();
		Status status = new Status(IStatus.ERROR, GeneratePlugin.PLUGIN_ID,
				IStatus.ERROR, msg, null);
		log.log(status);
	}

	/**
	 * Return the target explorer program configured in
	 * EasyExplorePreferencePage.
	 * 
	 * @return String
	 */
	public String getTarget() {
		return getPreferenceStore().getString(
				EasyExplorePreferencePage.P_TARGET);
	}

	/**
	 * Return the target command program configured in
	 * EasyExplorePreferencePage.
	 * 
	 * @return String
	 */
	public String getCommand() {
		return getPreferenceStore().getString(
				EasyExplorePreferencePage.P_COMMAND);
	}

	/**
	 * Tells whether this platform is currently supported. The implementation of
	 * this method must be in sync with the implementation of <a
	 * href="#initializeDefaultPreferences(IPreferenceStore)"
	 * >initializeDefaultPreferences(IPreferenceStore)</a>.
	 */
	public boolean isSupported() {
		String osName = System.getProperty("os.name").toLowerCase();
		return ((osName.indexOf("windows") != -1) || (osName.indexOf("mac") != -1));
	}

	/**
	 * 显示错误信息对话框
	 * 
	 * @param message
	 */
	public static void showErrorDialog(String message) {
		MessageBox messageBox = new MessageBox(PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getShell(), SWT.ICON_ERROR | SWT.OK);
		messageBox.setText(ResourceString
				.getResourceString("dialog.title.error"));
		messageBox.setMessage(ResourceString.getResourceString(message));
		messageBox.open();
	}

	public static void showExceptionDialog(Throwable e) {
		IStatus status = new Status(IStatus.ERROR, GeneratePlugin.PLUGIN_ID, 0,
				e.toString(), e);

		GeneratePlugin.log(e);

		// ErrorDialog.openError(PlatformUI.getWorkbench()
		// .getActiveWorkbenchWindow().getShell(),
		// ResourceString.getResourceString("dialog.title.error"),
		// ResourceString.getResourceString("error.plugin.error.message"),
		// status);
	}

	public static Image getImage(String key) {
		return getDefault().getImageRegistry().get(key);
	}

}
